package pages

import (
	"time"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/yourusername/tinput/internal/config"
)

// NewConfigPage 创建配置页面
func NewConfigPage(window fyne.Window) fyne.CanvasObject {
	// 获取当前配置
	currentConfig := config.GetConfig()

	// 创建DeepSeek API密钥输入框
	apiKeyEntry := widget.NewPasswordEntry()
	apiKeyEntry.SetPlaceHolder("Please input DeepSeek API Key")
	apiKeyEntry.SetText(currentConfig.DeepSeekKey)
	if currentConfig.CorrectionEngine != "deepseek" {
		apiKeyEntry.Hide()
	}

	// 创建模型类型选择
	modelTypeEntry := widget.NewSelect([]string{"deepseek-chat", "deepseek-coder"}, nil)
	// 如果当前模型类型为空，设置默认值
	if currentConfig.ModelType == "" {
		modelTypeEntry.SetSelected("deepseek-chat")
	} else {
		modelTypeEntry.SetSelected(currentConfig.ModelType)
	}
	if currentConfig.CorrectionEngine != "deepseek" {
		modelTypeEntry.Hide()
	}

	// 创建账号输入框
	accountEntry := widget.NewEntry()
	accountEntry.SetPlaceHolder("Please input account")
	if currentConfig.CorrectionEngine != "default" {
		accountEntry.Hide()
	}
	accountEntry.SetText(currentConfig.Token)

	// 创建密码输入框

	// homeurl, _ := url.Parse("https://dev-5genhwgo604cfadc-1251845214.tcloudbaseapp.com/web/#/register")
	// 创建超链接提示
	// linkText := widget.NewHyperlink("Click to go to the official website to get the account", homeurl)
	// if currentConfig.CorrectionEngine != "default" {
	// 	linkText.Hide()
	// }

	// TODO "default",
	// 创建引擎选择单选框
	engineRadio := widget.NewRadioGroup([]string{"default", "deepseek"}, func(value string) {
		// 更新DeepSeek密钥输入框的可见性
		if value == "deepseek" {
			apiKeyEntry.Show()
			modelTypeEntry.Show()
			accountEntry.Hide()
			// linkText.Hide()
		} else {
			apiKeyEntry.Hide()
			modelTypeEntry.Hide()
			accountEntry.Show()
			// linkText.Show()
		}
	})
	engineRadio.SetSelected(currentConfig.CorrectionEngine)

	// 创建保存按钮
	saveButton := widget.NewButton("Save Config", func() {
		newConfig := config.Config{
			CorrectionEngine: engineRadio.Selected,
			DeepSeekKey:      apiKeyEntry.Text,
			ModelType:        modelTypeEntry.Selected,
			OpenAIKey:        currentConfig.OpenAIKey,
			Token:            currentConfig.Token,
		}

		// 如果选择default引擎，进行登录请求
		if engineRadio.Selected == "default" {
			account := accountEntry.Text

			if account == "" {
				// 显示错误提示
				popUp := widget.NewModalPopUp(
					widget.NewLabel("Account cannot be empty"),
					window.Canvas(),
				)
				popUp.Show()
				time.AfterFunc(2*time.Second, func() {
					popUp.Hide()
				})
				return
			}

			// 发送登录请求
			go func() {

				// 提取token
				// 保存token到配置
				newConfig.Token = accountEntry.Text
				config.SetConfig(newConfig)

				// 显示成功提示
				go func() {
					popUp := widget.NewModalPopUp(
						widget.NewLabel("Configuration saved"),
						window.Canvas(),
					)
					popUp.Show()
					time.AfterFunc(2*time.Second, func() {
						popUp.Hide()
					})
				}()
			}()
			return
		}

		// 保存配置
		config.SetConfig(newConfig)

		// 显示保存成功提示
		popUp := widget.NewModalPopUp(
			widget.NewLabel("Config saved"),
			window.Canvas(),
		)
		popUp.Show()

		// 2秒后自动关闭弹窗
		time.AfterFunc(2*time.Second, func() {
			popUp.Hide()
		})
	})

	// 创建表单布局
	form := container.NewVBox(
		widget.NewLabel("Correction Engine"),
		engineRadio,
		widget.NewLabel("Username"),
		accountEntry,
		// linkText,
		widget.NewLabel("DeepSeek API Key"),
		apiKeyEntry,
		widget.NewLabel("Model Type"),
		modelTypeEntry,
		saveButton,
		widget.NewSeparator(), // 添加分隔线
		widget.NewLabel("Version: v0.0.1-alpha-250917 | © 2025 GG Org"), // 添加版本和作者信息
	)

	// 添加边距
	content := container.NewPadded(form)

	return content
}
